package com.hhsjdesign.dynamicui.core.widgetimpl.businesswidget

import android.content.Context
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.viewpager2.widget.ViewPager2
import com.hhsjdesign.dynamicui.annoprocess.AWidgetType
import com.hhsjdesign.dynamicui.core.widgetimpl.BusinessWidget
import com.hhsjdesign.dynamicui.businessdata.IViewable
import com.hhsjdesign.dynamicui.core.property.ShapeProperty
import com.hhsjdesign.dynamicui.core.widgetdata.WidgetData
import com.hhsjdesign.dynamicui.core.property.widget.BannerGWidgetProperty
import com.hhsjdesign.dynamicui.core.property.WidgetSpecProperty
import com.hhsjdesign.dynamicui.core.widgetdata.WidgetTypeConst
import com.hhsjdesign.dynamicui.core.widgetimpl.businesswidget.adapter.GridAdapter

/**
 * 网格轮播组件
 * BannerGridProperty
 */
@AWidgetType(WidgetTypeConst.BANNER_GRID)
class BannerGWidget(
    context: Context,
    data: WidgetData
) : BusinessWidget(context, data) {

    var viewPager: ViewPager2? = null
    var indicator: View? = null
    var defaultImage: ImageView? = null
    private var adapter: GridAdapter? = null
    private val businessItemWidgetList: MutableList<IViewable<*>>? = null

    override fun createMainView(specProperty: WidgetSpecProperty): View {
        val property: BannerGWidgetProperty =
            specProperty as BannerGWidgetProperty

        val contentView =
            if (property.indicatorLayoutType == 0) FrameLayout(context!!) else LinearLayout(
                context
            ).apply {
                orientation = LinearLayout.VERTICAL
            }

        viewPager = ViewPager2(context)
        contentView.addView(viewPager)

        if (property.defaultBgUrl != null) {
            defaultImage = ImageView(context)
            defaultImage!!.visibility = View.GONE
            contentView.addView(defaultImage)
        }

        if (property.indicatorProperty != null) {
            indicator = View(context)
            contentView.addView(indicator)
        }

        return contentView
    }

    override fun layout(property: ShapeProperty, parentType: String) {
    }

    override fun loadLocalBusiness() {
        if (adapter == null) {
            val specProperty: BannerGWidgetProperty =
                widgetData.specProperty as BannerGWidgetProperty
            adapter = GridAdapter(
                context,
                widgetData.itemPropertyList!![0].itemWidgetType,
                widgetData.itemPropertyList!![0].itemPropertyModel,
                widgetData.business!!,
                businessItemWidgetList!!,
                specProperty.rowNumber,
                specProperty.columnNumber
            )
        }
    }

    override fun loadRemoteBusiness() {

    }

}